// standard libraries
#include <string>
#include <vector>

// custom libraries
#include "..\..\include\graphics\graphics_renderer.h"
#include "..\..\include\graphics\font.h"
#include "..\..\include\graphics\image.h"

#include "..\..\include\game_manager.h"
#include "..\..\include\resource_manager.h"
#include "..\..\include\settings.h"
#include "..\..\include\user_controller.h"

#include "..\..\include\game_state\exit_state.h"
#include "..\..\include\game_state\menu_state.h" // matching header


// ==================================
//     SINGLETON IMPLEMENTATION
// ==================================

MenuState* MenuState::Instance()
{
	// Meyers singleton
	static MenuState obj;
	return &obj;
}

MenuState::MenuState() 
{
	timesOpened_ = 0;
	currentSelection_ = 0;

	backgroundImage_ = 0;  // <-- Will get loaded in
	textFont_        = 0;  // <-- MenuState::onOpen()
}

MenuState::MenuState(const MenuState& other) {}
MenuState::~MenuState() {}
MenuState& MenuState::operator=(const MenuState& other) { return *this; } 


// ==================================
//          OPEN and CLOSE
// ==================================

void MenuState::onOpen()
{
	if (timesOpened_++ > 0)
		return;

	ResourceManager* rm = ResourceManager::Instance();
	Settings* set = Settings::Instance();

	std::string imgPath = set->getMenuBackgroundFilepath();
	backgroundImage_ = rm->getImage(imgPath);

	std::string fontPath = set->getFontFilepath();
	textFont_ = rm->getFont(fontPath);

	gameOptions_.push_back("English");
	gameOptions_.push_back("German");
	gameOptions_.push_back("French");
	gameOptions_.push_back("Romanian");
}

void MenuState::onClose()
{
	if (--timesOpened_ > 0)
		return;

	ResourceManager* rm = ResourceManager::Instance();

	rm->release(backgroundImage_);
	backgroundImage_ = 0;

	rm->release(textFont_);
	textFont_ = 0;

	gameOptions_.clear();
}


// ==================================
//          ENTER and EXIT
// ==================================

void MenuState::onEnter() {}
void MenuState::onExit() {}


// ==================================
//         UPDATE and RENDER
// ==================================

void MenuState::update(GameManager* game, UserController* userController)
{
	userController->scanButtons();

	if (userController->isButtonPressed(UserController::EXIT)) {
		game->pushState(ExitState::Instance());
	} else if (userController->isButtonPressed(UserController::UP)) {
		if (--currentSelection_ < 0)
			currentSelection_ = gameOptions_.size() - 1;
	} else if (userController->isButtonPressed(UserController::DOWN)) {
		if (++currentSelection_ == (int)gameOptions_.size())
			currentSelection_ = 0;
	} else if (userController->isButtonPressed(UserController::SELECT)) {
		;
	}
}

void MenuState::render(GraphicsRenderer* graphicsRenderer)
{
	int textSize = 12;
	unsigned int rgbaSelected    = 0xFFFF00FF; // Yellow
	unsigned int rgbaNotSelected = 0x808080FF; // Gray

	graphicsRenderer->drawImage(*backgroundImage_, 0, 0);
	
	int x, y;
	unsigned int color;
	for (int i = 0, s = gameOptions_.size(); i < s; i++) {
		x = 60; 
		y = 260 + (textSize * i);

		if (i == currentSelection_)
			color = rgbaSelected;
		else
			color = rgbaNotSelected;
		
		graphicsRenderer->printText(gameOptions_[i], x, y, 
			*textFont_, textSize, color);
	}

	graphicsRenderer->render();
}
